package som.simon.reactor;

import reactor.core.publisher.Flux;

import java.util.concurrent.atomic.AtomicInteger;

public class TransformMain {

    public static void main(String[] args) {

        // transform();

        AtomicInteger num = new AtomicInteger();

        Flux<Integer> flux = Flux.range(1, 4)
                .transformDeferred(vs -> {
                    if (num.incrementAndGet() == 1) {
                        return vs.map(v -> v * 100);
                    }
                    return vs;
                });

        flux.subscribe(v -> System.out.println("one: v = " + v));
        flux.subscribe(v -> System.out.println("two: v = " + v));
        // one: v = 100
        // one: v = 200
        // one: v = 300
        // one: v = 400
        // two: v = 1
        // two: v = 2
        // two: v = 3
        // two: v = 4
    }

    private static void transform() {
        AtomicInteger num = new AtomicInteger();

        Flux<Integer> flux = Flux.range(1, 4)
                .transform(vs -> {
                    if (num.incrementAndGet() == 1) {
                        return vs.map(v -> v * 100);
                    }
                    return vs;
                });

        flux.subscribe(v -> System.out.println("one: v = " + v));
        flux.subscribe(v -> System.out.println("two: v = " + v));
        // one: v = 100
        // one: v = 200
        // one: v = 300
        // one: v = 400
        // two: v = 100
        // two: v = 200
        // two: v = 300
        // two: v = 400
    }

}
